//
//  LearnTableViewController.swift
//  SafeFoodManagerDemo
//
//  Created by 振轩 on 2018/4/17.
//  Copyright © 2018年 bob. All rights reserved.
//

import UIKit
let Learn = "learn"
class LearnTableViewController: SwiftBaseTableViewController {
    
    var data:[PlayRecordModel]? = [ PlayRecordModel]()
    

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "我的学习"
        tableView.register(UINib.init(nibName: "LearnListTableViewCell", bundle: nil), forCellReuseIdentifier: Learn)
        tableView.rowHeight = 100
        tableView.tableFooterView = UIView()
         //header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
       // self.tableView!.mj_header = header
        loadData()
    }
    
    
    
//    @objc func headerRefresh(){
//        print("下拉刷新.")
//        sleep(1)
//        //重现生成数据
//       loadData()
//        //重现加载表格数据
//        self.tableView!.reloadData()
//        //结束刷新
//        self.tableView!.mj_header.endRefreshing()
//    }
    
  

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.data?.count ?? 0
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: Learn, for: indexPath) as! LearnListTableViewCell

        cell.mode = data?[indexPath.row]

        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let mode = data![indexPath.row]
        
        let vc = LiveViewControllerViewController()
        let mode1:ReqMode  = ReqMode()
        mode1.staffId = LoginStatuesModel.getStaffId()
        mode1.staffType = LoginStatuesModel.getUserType()
        mode1.courseId = mode.id
        vc.currentPlayTime = mode.currentPlayTime
        vc.ReqM = mode1
        present(vc, animated: true, completion: nil)
        
    }
    
    override func loadData(){
        
        var params:[String:AnyObject] = [String:AnyObject]()
        params["type"] = 1 as AnyObject
        params["staffId"] = LoginStatuesModel.getStaffId() as AnyObject
        params["staffType"] = LoginStatuesModel.getUserType() as AnyObject
        params["pageOn"] = 1 as AnyObject
        params["pageSize"] = PageSize as AnyObject
        params["currentPage"] = currentPage as AnyObject
        
        print(params)
        
        let API:BaseReqApi = BaseReqApi.init(requestUrl: "/index.php/FoodCheckApi/coursePlayRecord.json", andrequestTime: 5, andParams: params as [AnyHashable : Any], andRequestMethod: YTKRequestMethod.POST, andCache: false, andCacheTime: 0, andPostToken: true)
        
        API.starRequest { (status, mess, response) in
            
            guard let dic:Dictionary = response as? [String : AnyObject] else{
                return
            }
            print(dic)
            
           
            
            if status.rawValue == 1  {
                let mode1  = PlayRecordInfoMode.init(DIC: dic)
                
               
                if self.isLoadmore{
                    
                    self.data = self.data! + (mode1.data as? [PlayRecordModel])!
                    
                }else{
                    
                    
                    self.data = mode1.data as? [PlayRecordModel]
                    
                }
                
                if (mode1.data?.count)! < self.PageSize{
                    
                    self.isFinish = true
                    
                }
                self.Count = (self.data?.count)!
                
                
                if self.data?.count == 0 {
                    self.Datatype = REQuestDataType.NODATAType
                    
                    
                }else{
                    
                    self.Datatype = REQuestDataType.NORMALLType
                }
                
                
                self.tableView.reloadData()
                
            }else if (status.rawValue == 404){
                
                UIAlertView.bk_show(withTitle: mess, message: "", cancelButtonTitle: "登陆", otherButtonTitles: ["取消"], handler: { (alertView, buttonIndex) in
                    if(buttonIndex == 0){
                        
                        
                        self.gotoLogin()
                        
                    }
                })
                
                
                
                
                
            }
        }
    }
    


}



///------------------播放记录--------------------









///------------------播放记录--------------------






